package io.renren.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.renren.controller.AbstractController;
import io.renren.dao.*;
import io.renren.entity.*;
import io.renren.service.ActFlowService;
import io.renren.service.ClbxService;
import io.renren.service.SysUploadService;
import io.renren.utils.ShiroUtils;
import io.renren.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Transactional(propagation= Propagation.REQUIRED)
@Service("clbxService")
public class ClbxServiceImpl extends AbstractController implements ClbxService {
	@Autowired
	private ClbxDao clbxDao;
	@Autowired
	private ClbxDetDao clbxDetDao;
	@Autowired
	private ActFlowService actFlowService;
	@Autowired
	private ActAssignDao actAssignDao;

	@Autowired
	private SysUploadService sysUploadService;

	@Autowired
	private UserDao userDao;

	@Autowired
	private ClbxUserdetDao clbxUserdetDao;
	@Override
	public List<ClbxEntity> queryList(Map<String, Object> map){
		return clbxDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return clbxDao.queryTotal(map);
	}

	@Override
	public void delete(String clbxId){
		clbxDao.delete(clbxId);
	}

	@Override
	public void deleteBatch(String[] bxIds){
		for(int i=0;i<bxIds.length; i++){
			ClbxEntity clbxEntity = clbxDao.queryObject(bxIds[i]);
			List<SysUploadEntity>	SysOssEntityList = sysUploadService.queryListByfileId(clbxEntity.getFileId());
			if(SysOssEntityList!=null&&SysOssEntityList.size()>0){
				for(SysUploadEntity list: SysOssEntityList){
					String url = list.getUrl();
					new File(url).delete();
				}
			}
			sysUploadService.deleteByfileId(clbxEntity.getFileId());
			List<ClbxDetEntity> clbxDetEntitylist =  clbxDao.queryDetList(bxIds[i]);
			if(clbxDetEntitylist!=null&&clbxDetEntitylist.size()>0){
				for(ClbxDetEntity list:clbxDetEntitylist){
					clbxDetDao.delete(list.getBxdetId());
				}
			}
		}
		clbxDao.deleteBatch(bxIds);
	}
//废弃
	@Override
	public void addUser(Integer[] userIds,String clbx_id) {
		if(clbx_id==null&&"".equals(clbx_id)){
			clbx_id=String.valueOf(System.currentTimeMillis());
		}

		for(int id: userIds){
			UserEntity userEntity =  userDao.queryObject(id);
			ClbxUserdetEntity clbxUserdetEntity = new ClbxUserdetEntity();

		}
	}

	public Map<String, Object> queryObject(String bxId){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("clbx",clbxDao.queryObject(bxId));
		List<ClbxDetEntity> clbxDetList =  clbxDao.queryDetList(bxId);
		if(clbxDetList!=null&&clbxDetList.size()>0){
			map.put("clbxDetList",clbxDetList);
		}
		return map ;
	}
	@Override
	public void save(JSONObject params){
		ClbxEntity clbxEntity =  params.getObject("clbx",ClbxEntity.class);
		clbxEntity.setClbxId(String.valueOf(System.currentTimeMillis()));
		clbxEntity.setFileId(String.valueOf(System.currentTimeMillis()));
		List<ClbxDetEntity> ClbxDetEntityList = JSON.parseArray(params.get("clbxDetList").toString(), ClbxDetEntity.class);
		SysUserEntity user = (SysUserEntity) ShiroUtils.getSubject().getSession().getAttribute("user");

		clbxEntity.setCompanyId(user.getCompanyId());
		clbxEntity.setCusername(user.getUsername());
		clbxEntity.setDeptId(user.getDeptNo());
		clbxEntity.setCompany(user.getCompany());
		clbxEntity.setDept(user.getDept());
		clbxEntity.setUserdesc(user.getUserdesc());
		clbxEntity.setPost(user.getPost());

		clbxDao.save(clbxEntity);
		if(ClbxDetEntityList!=null&&ClbxDetEntityList.size()>0) {
			for (ClbxDetEntity list : ClbxDetEntityList) {
				list.setClbxId(clbxEntity.getClbxId());
				clbxDetDao.save(list);
			}
		}
	}

	@Override
	public void update(JSONObject params){
		ClbxEntity clbxEntity =  params.getObject("clbx",ClbxEntity.class);
		clbxDao.update(clbxEntity);
		List<ClbxDetEntity> clbxDetEntityList =  clbxDao.queryDetList(clbxEntity.getClbxId());
		if(clbxDetEntityList!=null&&clbxDetEntityList.size()>0){
			for(ClbxDetEntity list:clbxDetEntityList){
				clbxDetDao.delete(list.getBxdetId());
			}
		}
		List<ClbxDetEntity> ClbxDetEntityList = JSON.parseArray(params.get("clbxDetList").toString(), ClbxDetEntity.class);
		if(ClbxDetEntityList!=null&&ClbxDetEntityList.size()>0) {
			for (ClbxDetEntity list : ClbxDetEntityList) {
				list.setClbxId(clbxEntity.getClbxId());
				clbxDetDao.save(list);
			}
		}
	}

	@Override
	@Transactional
	public String submit(Integer bxId ){
		ClbxEntity entity=clbxDao.queryObject(bxId);
		Map<String,Object> map=new HashMap();
		map.put("processName","spmc07");//数据字段中spmc07代表报销
		String processName="spmc07";
		List<ActAssignEntity> listActAssign=actAssignDao.queryBYName(processName);
		if(listActAssign==null||listActAssign.size()==0){
			return "请在审批配置中配置审批条件！";
		}
		ActAssignEntity actAssign=listActAssign.get(0);
		Map mp=actFlowService.submitFlow(actAssign,entity,processName);
		if(mp.get("msg")!=null){
			String msg=mp.get("msg").toString();
			if(StringUtils.isNotBlank(msg)){
				return msg;
			}
		}
		entity=(ClbxEntity)mp.get("obj");
		entity.setTjDate(new Date());
		entity.setTjr(getUser().getUsername());
		clbxDao.update(entity);
		return "";
	}


	@Override
	@Transactional
	public void process(ClbxEntity entity){
		Map<String, Object> map=new HashMap<>();
		map.put("piid",entity.getPiid());
		map.put("remark",entity.getRemark()==null?"":entity.getRemark());
		map.put("nextTo",entity.getNextTo());
		Object obj=actFlowService.saveFlow(map,entity,"agree","spmc07");
		entity=(ClbxEntity)obj;
		clbxDao.update(entity);

	}

	@Override
	@Transactional
	public void reject(ClbxEntity entity){

		Map<String, Object> map=new HashMap<>();
		map.put("piid",entity.getPiid());
		map.put("remark",entity.getRemark()==null?"":entity.getRemark());
		map.put("nextTo",entity.getNextTo());
		Object obj=actFlowService.saveFlow(map,entity,"reject","spmc07");
		entity=(ClbxEntity)obj;
		entity.setPistatus("10");//设置为拒绝的状态
		clbxDao.update(entity);


	}


}
